from flask import request

from handle.CouresHandle import couSearch, couAdd, couDelete, couAllSearch, couEdit
from publicHandle.publicHandle import returnData, searchUserId


class apiCourse():
    idCount = 5

    def __init__(self, app):
        # 查询所有课程
        @app.route("/allCourse", methods=["POST"])
        def allCou():
            if searchUserId(request.headers.get("userid")) :
                return couAllSearch()
            else:
                return returnData({}, "请先登录", 401)

        # 增加课程
        @app.route("/addCourse", methods={"POSt"})
        def addCou():
            if searchUserId(request.headers.get("userid")) :
                courseId = request.values.get("courseId")
                name = request.values.get("name")
                teacherName = request.values.get("teacherName")
                credit = request.values.get("credit")
                time = request.values.get("time")
                return couAdd(courseId, name, teacherName, credit, time)
            else:
                return returnData({}, "请先登录", 401)

        # 删除课程
        @app.route("/deleteCourse", methods={"POST"})
        def deleteCou():
            if searchUserId(request.headers.get("userid")) :
                courseId = request.values.get("courseId")
                return couDelete(courseId)
            else:
                return returnData({}, "请先登录", 401)

        # 修改课程信息
        @app.route("/editCourse", methods={"POST"})
        def editCou():
            if searchUserId(request.headers.get("userid")) :
                courseId = request.values.get("courseId")
                name = request.values.get("name")
                teacherName = request.values.get("teacherName")
                credit = request.values.get("credit")
                time = request.values.get("time")
                return couEdit(courseId, name, teacherName, credit, time)
            else:
                return returnData({}, "请先登录", 401)

        # 搜索课程
        @app.route("/searchCourse", methods={"POST"})
        def searchCou():
            if searchUserId(request.headers.get("userid")) :
                name = request.values.get("name")
                return couSearch(name)
            else:
                return returnData({}, "请先登录", 401)
